package com.kxmall.web.controller.task;

import cn.dev33.satoken.annotation.SaIgnore;
import com.kxmall.common.core.controller.BaseRiderController;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.rider.domain.vo.KxRiderOrderVo;
import com.kxmall.rider.domain.vo.RiderOrderStatisticalDTO;
import com.kxmall.web.controller.rider.service.RiderWxLoginService;
import com.kxmall.web.controller.task.service.TaskCenterService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @author kaixin
 * @version 1.0
 * @date 2023/9/23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/rider/task")
public class TaskCenterController extends BaseRiderController {

    private final TaskCenterService taskCenterService;

    /**
     * 根据配送状态获取订单信息列表
     *
     * @return 结果
     */
    @GetMapping("/list")
    public TableDataInfo<KxRiderOrderVo> list(
            Integer status,
            Integer page,
            Integer limit) {
        Long riderId = getRiderLoginUser().getUserId();
        return taskCenterService.list(status,page,limit, riderId);
    }



    /**
     * 开始配送
     *
     * @return 结果
     */
    @GetMapping("/begin")
    public R<String> begin(Long riderOrderId,
                           BigDecimal lng,
                           BigDecimal lat) {
        Long riderId = getRiderLoginUser().getUserId();
        return R.ok("操作成功",taskCenterService.begin(riderOrderId,lng,lat, riderId));
    }



    /**
     * 异常配送
     *
     * @return 结果
     */
    @GetMapping("/abnormal")
    public R<String> abnormal(Long riderOrderId,
                              String reason,
                              BigDecimal lng,
                              BigDecimal lat) {
        Long riderId = getRiderLoginUser().getUserId();
        return R.ok("操作成功",taskCenterService.abnormal(riderOrderId,reason,lng,lat, riderId));
    }



    /**
     * 订单详情
     *
     * @return 结果
     */
    @SaIgnore
    @GetMapping("/detail")
    public R<KxRiderOrderVo> detail(Long riderOrderId) {
        Long riderId = getRiderLoginUser().getUserId();
        return R.ok("操作成功！",taskCenterService.detail(riderOrderId, riderId));
    }
    /**
     * 完成配送
     *
     * @return 结果
     */
    @GetMapping("/complete")
    public R<String> complete(Long riderOrderId,
                                      BigDecimal lng,
                                      BigDecimal lat) {
        Long riderId = getRiderLoginUser().getUserId();
        return R.ok("操作成功！",taskCenterService.complete(riderOrderId,lng,lat,riderId));
    }

    /**
     * 列表统计
     *
     * @return 结果
     */
    @GetMapping("/statistical")
    public R<RiderOrderStatisticalDTO> statistical() {
        Long riderId = getRiderLoginUser().getUserId();
        return R.ok("操作成功！",taskCenterService.statistical(riderId));
    }



    /**
     * 收入列表
     *
     * @return 结果
     */
    @GetMapping("/income")
    public TableDataInfo<KxRiderOrderVo> income(Integer pageNum,
                                                Integer limit) {
        Long riderId = getRiderLoginUser().getUserId();
        return taskCenterService.income(pageNum,limit,riderId);
    }



    /**
     * 统计金额
     *
     * @return 结果
     */
    @GetMapping("/countIncome")
    public R<BigDecimal> countIncome() {
        Long riderId = getRiderLoginUser().getUserId();
        return R.ok(taskCenterService.countIncome(riderId));
    }

}
